//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackVarianceSurface.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Math;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (boost::shared_ptr<QuantLib::BlackVarianceSurface>& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::BlackVarianceSurface& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    _BlackVarianceSurfaceOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (CBlackVarianceSurface^ copy) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (PLATFORM::Type^ t) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVarianceSurface::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::Handle<QuantLib::BlackVarianceSurface>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
	_phBlackVarianceSurface = &childNative;
	_ppBlackVarianceSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceSurface>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    _BlackVarianceSurfaceOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::Handle<QuantLib::BlackVarianceSurface> childNative)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
	_phBlackVarianceSurface = &childNative;
	_ppBlackVarianceSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceSurface>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::BlackVarianceSurface childNative)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (new QuantLib::BlackVarianceSurface (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::~CBlackVarianceSurface ()
{
    if (_ppBlackVarianceSurface != NULL)
    {
	    delete _ppBlackVarianceSurface;
        _ppBlackVarianceSurface = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::!CBlackVarianceSurface ()
{
    if (_ppBlackVarianceSurface != NULL)
    {
	    delete _ppBlackVarianceSurface;
    }
}
QuantLib::BlackVarianceSurface& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetReference ()
{
    if (_ppBlackVarianceSurface == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackVarianceSurface;
}
boost::shared_ptr<QuantLib::BlackVarianceSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetShared ()
{
    if (_ppBlackVarianceSurface == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackVarianceSurface;
}
QuantLib::BlackVarianceSurface* Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetPointer ()
{
    if (_ppBlackVarianceSurface == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackVarianceSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVarianceSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetHandle ()
{
	if (_phBlackVarianceSurface == NULL)
	{
		_phBlackVarianceSurface = new Handle<QuantLib::BlackVarianceSurface> (*_ppBlackVarianceSurface);
	}
	return *_phBlackVarianceSurface;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::HasNative () 
{
	return (_ppBlackVarianceSurface != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppBlackVarianceSurface)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBlackVarianceSurface)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceSurface)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceSurface)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

